package com.example.service.impl;

import com.example.entity.AxdRiskUser;
import com.example.entity.LoginUser;
import com.example.entity.ResponseResult;
import com.example.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import java.util.Objects;

/*
 *  @author 雨安
 *  类名： LoginServiceImpl
 *  创建时间：2024/3/18
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public ResponseResult login(AxdRiskUser riskUser) {
        //封装账号密码
        UsernamePasswordAuthenticationToken
                //传入账号密码
                authenticationToken = new UsernamePasswordAuthenticationToken(riskUser.getUserName(),riskUser.getPassword());
        //AuthenticationManager进行用户认证
        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
        //如果认证未通过，给出对应的提示
        if (Objects.isNull(authenticate)){
            //为null抛出异常
            throw new RuntimeException("认证未通过,登录失败!");
        }
        //把完整的用户信息存入redis userid作为key
        return new ResponseResult(200,"登录成功","我的Data");
    }
}
